package com.lbmb.photodrawaimanager.utils;

/**
 * @author hanWang
 * @Title:
 * @Package
 * @Description:
 * @date 2023/8/26:43 下午
 */

import com.lbmb.photodrawaimanager.enums.ResponseEnum;

import java.io.Serializable;
import java.util.HashMap;

public class Response<T> implements Serializable {
    public static final int SUCCESS_CODE = 200;
    public static final int FAIL_CODE = 300;
    public static final int PARAM_ILLEGAL_CODE = 301;
   // @ApiModelProperty("响应码,200为请求成功,300为运行时异常,301为参数不合法,-200为token不合法跳登录页;其他异常均为业务异常")
    private int code;
  //  @ApiModelProperty("响应信息")
    private String msg;
   // @ApiModelProperty("业务数据")
    private T data;
   // @ApiModelProperty("全局链路请求id")
    private String rid;

    public Response() {
    }

    public Response(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Response(ErrorCodeEnums errorCodeEnums) {
        this.code = errorCodeEnums.getCode();
        this.msg = errorCodeEnums.getMessage();
    }

    public Response(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> Response<T> ok() {
        return restResult(null, 200, (String)null);
    }

    public static <T> Response<T> ok(T data) {
        return restResult(data, ResponseEnum.COMMON_SUCCESS.getCode(), ResponseEnum.COMMON_SUCCESS.getMessage());
    }

    public static <T> Response<T> ok(T data, int i, String msg) {
        return restResult(data, 200, (String)null);
    }

    public static <T> Response<T> ok(String key, T data) {
        HashMap<String, String> map = new HashMap();
        map.put(key, String.valueOf(data));
        return restResult((T)map, 200, (String)null);
    }

    public static <T> Response<T> ok(T data, String msg) {
        return restResult(data, 200, msg);
    }

    public static <T> Response<T> result(T data, int code, String msg) {
        return restResult(data, code, msg);
    }

    public static <T> Response<T> failed() {
        return restResult(null, 300, null);
    }

    public static <T> Response<T> failed(String msg) {
        return restResult(null, 300, msg);
    }

    public static <T> Response<T> failed(int code, String msg) {
        return restResult(null, code, msg);
    }

    public static <T> Response<T> failed(ErrorCodeEnums errorCodeEnums) {
        return restResult(null, errorCodeEnums.getCode(), errorCodeEnums.getMessage());
    }

    public static <T> Response<T> failed(T data) {
        return restResult(data, 300, (String)null);
    }

    public static <T> Response<T> failed(T data, String msg) {
        return restResult(data, 300, msg);
    }

    public static <T> Response<T> restResult(T data, int code, String msg) {
        Response<T> apiResult = new Response();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }

    public Boolean isSuccess() {
        return this.code == 200;
    }

    public static <T> boolean isNotSuccess(Response<T> response) {
        return !isSuccess(response);
    }

    public static <T> boolean isSuccess(Response<T> response) {
        return response != null && response.code == 200;
    }

    public static <T> Response<T> restfulResult(ResponseEnum respEnum, T data) {
        Response<T> apiResult = new Response();
        apiResult.setCode(respEnum.getCode());
        apiResult.setMsg(respEnum.getMessage());
        apiResult.setData(data);
        return apiResult;
    }

    public void setErrorCodeEnum(ErrorCodeEnums errorCodeEnums) {
        this.code = errorCodeEnums.getCode();
        this.msg = errorCodeEnums.getMessage();
    }

    public void setErrorCodeEnum(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static <T> ResponseBuilder<T> builder() {
        return new ResponseBuilder();
    }

    public int getCode() {
        return this.code;
    }

    public String getMsg() {
        return this.msg;
    }

    public T getData() {
        return this.data;
    }

    public String getRid() {
        return this.rid;
    }

    public void setCode(final int code) {
        this.code = code;
    }

    public void setMsg(final String msg) {
        this.msg = msg;
    }

    public void setData(final T data) {
        this.data = data;
    }

    public void setRid(final String rid) {
        this.rid = rid;
    }

    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof Response)) {
            return false;
        } else {
            Response<?> other = (Response)o;
            if (!other.canEqual(this)) {
                return false;
            } else if (this.getCode() != other.getCode()) {
                return false;
            } else {
                label49: {
                    Object this$msg = this.getMsg();
                    Object other$msg = other.getMsg();
                    if (this$msg == null) {
                        if (other$msg == null) {
                            break label49;
                        }
                    } else if (this$msg.equals(other$msg)) {
                        break label49;
                    }

                    return false;
                }

                Object this$data = this.getData();
                Object other$data = other.getData();
                if (this$data == null) {
                    if (other$data != null) {
                        return false;
                    }
                } else if (!this$data.equals(other$data)) {
                    return false;
                }

                Object this$rid = this.getRid();
                Object other$rid = other.getRid();
                if (this$rid == null) {
                    if (other$rid != null) {
                        return false;
                    }
                } else if (!this$rid.equals(other$rid)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(final Object other) {
        return other instanceof Response;
    }

    public int hashCode() {
        int PRIME = 1;
        int result = 1;
         result = result * 59 + this.getCode();
        Object $msg = this.getMsg();
        result = result * 59 + ($msg == null ? 43 : $msg.hashCode());
        Object $data = this.getData();
        result = result * 59 + ($data == null ? 43 : $data.hashCode());
        Object $rid = this.getRid();
        result = result * 59 + ($rid == null ? 43 : $rid.hashCode());
        return result;
    }

    public String toString() {
        return "Response(code=" + this.getCode() + ", msg=" + this.getMsg() + ", data=" + this.getData() + ", rid=" + this.getRid() + ")";
    }

    public Response(final int code, final String msg, final T data, final String rid) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.rid = rid;
    }

    public static class ResponseBuilder<T> {
        private int code;
        private String msg;
        private T data;
        private String rid;

        ResponseBuilder() {
        }

        public ResponseBuilder<T> code(final int code) {
            this.code = code;
            return this;
        }

        public ResponseBuilder<T> msg(final String msg) {
            this.msg = msg;
            return this;
        }

        public ResponseBuilder<T> data(final T data) {
            this.data = data;
            return this;
        }

        public ResponseBuilder<T> rid(final String rid) {
            this.rid = rid;
            return this;
        }

        public Response<T> build() {
            return new Response(this.code, this.msg, this.data, this.rid);
        }

        public String toString() {
            return "Response.ResponseBuilder(code=" + this.code + ", msg=" + this.msg + ", data=" + this.data + ", rid=" + this.rid + ")";
        }
    }
}
